package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.UmengRegistrar;
import com.umeng.message.entity.UMessage;

/**
 * 
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : WelcomeActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月15日 下午10:54:14
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 欢迎页面 }
 *
 */
@EActivity(R.layout.welcome)
public class WelcomeActivity extends BaseActivity implements OnPageChangeListener{
	@ViewById
	ViewPager viewPager;
	@ViewById
	ViewGroup viewGroup;
	@ViewById
	Button guide_button;
	
	//装点点的ImageView数组
	private ImageView[] tips;
	//装ImageView数组
	private ImageView[] mImageViews;
	//图片资源id
	private int[] imgIdArray ;
	
	@AfterViews
	public void init() {
		//载入图片资源ID
//		imgIdArray = new int[]{R.drawable.item01, R.drawable.item02, R.drawable.item03, R.drawable.item04};
		imgIdArray = new int[]{R.drawable.start_bg, R.drawable.guide_bg};
		//将点点加入到ViewGroup中
		tips = new ImageView[imgIdArray.length];
		for(int i=0; i<tips.length; i++){
			ImageView imageView = new ImageView(this);
	    	imageView.setLayoutParams(new LayoutParams(10,10));
	    	tips[i] = imageView;
	    	if(i == 0){
	    		tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
	    	}else{
	    		tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
	    	}
	    	
	    	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,  
                    LayoutParams.WRAP_CONTENT));
	    	layoutParams.leftMargin = 5;
	    	layoutParams.rightMargin = 5;
	    	viewGroup.addView(imageView, layoutParams);
		}
		
		
		//将图片装载到数组中
		mImageViews = new ImageView[imgIdArray.length];
		for(int i=0; i<mImageViews.length; i++){
			ImageView imageView = new ImageView(this);
			imageView.setBackgroundResource(imgIdArray[i]);
			mImageViews[i] = imageView;
		}
		
		//设置Adapter
		viewPager.setAdapter(new MyAdapter());
		//设置监听，主要是设置点点的背景
		viewPager.setOnPageChangeListener(this);
		//设置ViewPager的默认项, 设置为长度的100倍，这样子开始就能往左滑动
		viewPager.setCurrentItem(0);
		
		
		
		String device_token = UmengRegistrar.getRegistrationId(context);
		myprefs.device_token().put(device_token);
		LogUtil.e("token :", device_token);
	}
	@Click({R.id.guide_button})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.guide_button://开始体验
			//判断是否已经选择地区,如果没有,跳转到选择地区页面
			String region=myprefs.region().get();
			if(!TextUtils.isEmpty(region)){
//				Bundle bundle=new Bundle();
//				bundle.putInt("position", 0);
//				openActivity(MainTabActivity_.class,bundle);
				myprefs.position().put(0);
				openActivity(MainTabActivity_.class);
			}else{
				openActivity(ChooseRegionActivity_.class);
			}
			finish();
			break;
		default:
			break;
		}
	}
	
	
	
	
	/**
	 * 
	 * @Project : Fruit
	 * @Package : com.sondon.mayi.activity
	 * @Class : MyAdapter
	 * @Author : 蔡文锋
	 * @DateTime：2015年6月15日 下午10:54:32
	 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
	 * @Description : { viewpager适配器 }
	 *
	 */
	public class MyAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return mImageViews.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager)container).removeView(mImageViews[position % mImageViews.length]);
			
		}

		/**
		 * 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
		 */
		@Override
		public Object instantiateItem(View container, int position) {
//			LogUtil.d(TAG, "position :"+position);
			((ViewPager)container).addView(mImageViews[position % mImageViews.length], 0);
			return mImageViews[position % mImageViews.length];
		}
		
	}

	@Override
	public void onPageScrollStateChanged(int statue) {
//		LogUtil.d(TAG, "onPageScrollStateChanged   :"+statue);
	}

	@Override
	public void onPageScrolled(int position, float arg1, int x) {
//		LogUtil.d(TAG, "position :"+position+"arg1     :"+arg1+"             x     :"+x);
		if(position==(imgIdArray.length-1)){
			guide_button.setVisibility(View.VISIBLE);
		}else{
			guide_button.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onPageSelected(int position) {
//		LogUtil.d(TAG, "onPageSelected :"+position);
		setImageBackground(position % mImageViews.length);
		
	}
	
	/**
	 * 设置选中的tip的背景
	 * @param selectItems
	 */
	private void setImageBackground(int selectItems){
		for(int i=0; i<tips.length; i++){
			if(i == selectItems){
				tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
			}else{
				tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
			}
		}
	}
	
}
